﻿// 项目：	Guan.CMS.Domain - Guan.CMS
// 文件名：	CanAddUser.cs
// 
// 修改时间：2013-03-08 21:52
// 修改人：	Cooclsee

using System.Collections.Generic;
using System.Linq;
using Guan.CMS.CommandProcessor.Command;
using Guan.CMS.Core.Common;
using Guan.CMS.Data.Repositories;
using Guan.CMS.Domain.Commands.Account;
using Guan.CMS.Model.Entities;

namespace Guan.CMS.Domain.Handlers.Account
{
    public class CanAddUser : IValidationHandler<UserRegisterCommand>
    {
        private readonly IUserRepository _userRepository;
        public CanAddUser(IUserRepository userRepository)
        {
            this._userRepository = userRepository;
        }
        public IEnumerable<ValidationResult> Validate(UserRegisterCommand command)
        {
            var isUserExists = _userRepository.GetMany(c => c.Email == command.Email || c.UserName == command.UserName);
            var userExists = isUserExists as IList<User> ?? isUserExists.ToList();
            if (userExists.Any())
            {
                if (userExists.Any(c => c.Email==command.Email))
                {
                    yield return new ValidationResult("EMail", "此邮箱已注册。");
                }
                if (userExists.Any(c => c.UserName == command.UserName))
                {
                    yield return new ValidationResult("UserName", "用户名已存在。");
                }
            }
        }
    }
}